package com.cote.douhahou.pretloan.beans;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;

import com.cote.douhahou.pretloan.launcher.BaseContext;
import com.cote.douhahou.pretloan.utils.AkomapaLendUtil;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.cote.douhahou.pretloan.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class AppAkomapalendConfigCustomize {

    private static class In_akomapalendner {
        private static AppAkomapalendConfigCustomize ins_akomapalendtance = new AppAkomapalendConfigCustomize();
    }

    private AppAkomapalendConfigCustomize() {}

    public static AppAkomapalendConfigCustomize instance() {
        return In_akomapalendner.ins_akomapalendtance;
    }

    public static void par_akomapalendse(String jsonStr) {

        In_akomapalendner.ins_akomapalendtance = new Gson().fromJson(jsonStr, AppAkomapalendConfigCustomize.class);

        In_akomapalendner.ins_akomapalendtance.recommendAppList = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);

            JSONArray recommendAppListJsarray= jsonObject.optJSONArray("recomemendAppInfo");
            if (recommendAppListJsarray!=null){
                for (int i=0;i<recommendAppListJsarray.length();i++){
                    JSONObject recommendInfoJsobject = recommendAppListJsarray.optJSONObject(i);
                    if (recommendInfoJsobject!=null){
                        In_akomapalendner.ins_akomapalendtance.recommendAppList.add(RecommendAkomapalendAppInfo.getI_akomapalendnstance(recommendInfoJsobject.toString()));
                    }
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }finally {
            In_akomapalendner.ins_akomapalendtance.srcStr = jsonStr;
        }

    }

    //参数
    private String mobile;//客服电话
    private String email;//客服邮箱
    private String customerServiceHint;//客服提示文案
    private String agreementUrl;//协议链接
    private JsonArray accList;//测试账号
    private JsonArray walletChannels;//法语区绑卡渠道列表
    private String rejectPeriod;//借款失败后间隔时间
    private String showSysReview;//评分弹窗控制
    private AppAkomapalendConfigPermissions permissions;//Permission配置
    private String recommendFlag;//app推荐开关（YES/NO）
    private ArrayList<RecommendAkomapalendAppInfo> recommendAppList;//app推荐列表
    private String isShowWaitTips;//是否显示审核页提示文案
    private String waitTipsTitle;//审核页提示文案标题
    private String waitTipsText;//审核页提示文案

    private String bindAccountTipsTitle;//法语区绑卡页面提示文案标题
    private String bindAccountTipsDesc;//法语区绑卡页面提示文案

    private String srcStr;
    private ArrayList<AccountChaAkomapalendnnel> accountChannels;

    private String isAudit;
    private String homeTerm;

    public String[] getMobiles_akomapalend(){
        if (TextUtils.isEmpty(mobile)) return new String[0];
        return mobile.split(",");
    }

    public String[] getEmails_akomapalend(){
        if (TextUtils.isEmpty(email)) return new String[0];
        return email.split(",");
    }

    public String getCustomerServiceHint_akomapalend() {
        if (TextUtils.isEmpty(customerServiceHint)) return "";
        return customerServiceHint;
    }

    public String getAgreementUrl_akomapalend() {
        if (TextUtils.isEmpty(agreementUrl)) return "";
        return agreementUrl;
    }

    public String getTestToken_akomapalend(String mobile){
        if (accList == null || accList.size()<1) return "";
        for (int i=0;i<accList.size();i++){
            try {
                JSONObject accJson = new JSONObject(accList.get(i).toString());
                if (accJson.optString("accM").equalsIgnoreCase(mobile)){
                    return accJson.optString("accTk");
                }
            }catch (JSONException e){
                e.printStackTrace();
            }
        }
        return "";
    }

    public boolean isTestAccount_akomapalend(String mobile){
        return !TextUtils.isEmpty(getTestToken_akomapalend(mobile));
    }

    public boolean isTestAccount_akomapalend(){
        return !TextUtils.isEmpty(getTestToken_akomapalend(AkomapaLendUtil.mobile()));
    }

    private void initAccountChannels_akomapalend(){
        accountChannels = new ArrayList<>();
        if (walletChannels==null || walletChannels.isEmpty()) return;
        Gson gson = new Gson();
        for (int i=0;i<walletChannels.size();i++){
            AccountChaAkomapalendnnel channel = gson.fromJson(walletChannels.get(i).toString(), AccountChaAkomapalendnnel.class);
            accountChannels.add(channel);
        }
    }

    public ArrayList<AccountChaAkomapalendnnel> getAccountChannels_akomapalend(){
        if (!hasAccountChannel_akomapalend()) initAccountChannels_akomapalend();
        return accountChannels;
    }

    public Bitmap getChannelLogoByDesc_akomapalend(String channelDesc){
        if (!hasAccountChannel_akomapalend()) initAccountChannels_akomapalend();
        for (AccountChaAkomapalendnnel channel:accountChannels){
            if (channelDesc.equalsIgnoreCase(channel.getDesc_akomapalend())) return channel.getBitmap_akomapalend();
        }
        return null;
    }

    public Bitmap getChannelLogoByValue_akomapalend(String channelValue){
        if (!hasAccountChannel_akomapalend()) initAccountChannels_akomapalend();
        for (AccountChaAkomapalendnnel channel:accountChannels){
            if (channelValue.equalsIgnoreCase(channel.getValue_akomapalend())) return channel.getBitmap_akomapalend();
        }
        return null;
    }

    public void setChannelLogoByDesc_akomapalend(String desc, Bitmap logoBm){
        if (!hasAccountChannel_akomapalend()) initAccountChannels_akomapalend();
        for (AccountChaAkomapalendnnel channel:accountChannels){
            if (desc.equalsIgnoreCase(channel.getDesc_akomapalend())) channel.setBitmap_akomapalend(logoBm);
        }
    }

    public void setChannelLogoByValue_akomapalend(String value, Bitmap logoBm){
        if (!hasAccountChannel_akomapalend()) initAccountChannels_akomapalend();
        for (AccountChaAkomapalendnnel channel:accountChannels){
            if (value.equalsIgnoreCase(channel.getValue_akomapalend())) channel.setBitmap_akomapalend(logoBm);
        }
    }

    public boolean hasAccountChannel_akomapalend(){
        return accountChannels!=null && !accountChannels.isEmpty();
    }

    public String getRejectPeriod_akomapalend(Context context) {
        if (TextUtils.isEmpty(rejectPeriod)) return context.getString(R.string.days_akomapalend,7);
        try {

            return context.getString(R.string.days_akomapalend,Integer.parseInt(rejectPeriod));
        }catch (NumberFormatException e){
            e.printStackTrace();
            return context.getString(R.string.days_akomapalend,7);
        }
    }

    public String[] getHomePermission_akomapalend(){

        if (permissions == null) return new ArrayList<String>().toArray(new String[0]);
        ArrayList<String> home = permissions.homeP();
        return home.toArray(new String[0]);
    }

    public boolean getRecommendFlag_akomapalend(){
        if (TextUtils.isEmpty(recommendFlag)) return false;
        return recommendFlag.equalsIgnoreCase("YES");
    }

    public ArrayList<RecommendAkomapalendAppInfo> getRecommendAppList_akomapalend() {
        if (recommendAppList == null) new ArrayList<>();
        return recommendAppList;
    }

    public boolean showSysReview_akomapalend(){
        if (TextUtils.isEmpty(showSysReview)) return false;
        return showSysReview.equalsIgnoreCase("YES");
    }

    public String getBindAccountTipsTitle_akomapalend() {
        if (TextUtils.isEmpty(bindAccountTipsTitle)) return "";
        return bindAccountTipsTitle;
    }

    public String getBindAccountTipsDesc_akomapalend() {
        if (TextUtils.isEmpty(bindAccountTipsDesc)) return "";
        return bindAccountTipsDesc;
    }

    public String getOther_akomapalend(String pName){
        if (TextUtils.isEmpty(srcStr)) return "";

        try {
            JSONObject jsonObject = new JSONObject(srcStr);
            String result = jsonObject.optString(pName);
            if (TextUtils.isEmpty(result)) return "";
            return result;
        } catch (JSONException e) {
            e.printStackTrace();
            return "";
        }

    }

    public boolean getIsAudit() {
        if (TextUtils.isEmpty(isAudit)) return true;
        return !isAudit.equalsIgnoreCase("NO");
    }

    public String getHomeTerm(){
        if (TextUtils.isEmpty(homeTerm)) return BaseContext.instance().getString(R.string.days_akomapalend,120);
        return homeTerm;
    }

    public boolean isShowWaitTips() {
        if (TextUtils.isEmpty(isShowWaitTips) || TextUtils.isEmpty(waitTipsText)) return false;
        return isShowWaitTips.equalsIgnoreCase("YES");
    }

    public String getWaitTipsTitle() {
        if (TextUtils.isEmpty(waitTipsTitle)) return "";
        return waitTipsTitle;
    }

    public String getWaitTipsText() {
        if (TextUtils.isEmpty(waitTipsText)) return "";
        return waitTipsText;
    }

}
